// 引入接口文件
import { getList,getDetail,queryOrgMemberById,exportList,getTeam} from './reportClientListServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'

let teamList=[]
// orgId  agentConfigId  custName  custMobile  status  referrerName  referrerMobile
//  经济人  支持模糊搜索
// 经纪人电话 只支持全号搜索
// 报备客户姓名 支持模糊搜索
// 报备客户电话 只支持全号搜索
// 报备客户状态
const queryList = [
  {name: '报备项目', type: 'text', key: 'productName', value: '',placeholder: '支持模糊搜索',},
  {name: '经纪人', type: 'text', key: 'referrerName', value: '',placeholder: '支持模糊搜索',},
  {name: '经纪人电话', type: 'text', key: 'referrerMobile', value: '',placeholder: '只支持全号搜索',},
  {name: '团队名称', type: 'select', key: 'teamId', value: '',  notNeedAll: true,placeholder: '',list:[{id: '', name: '全部'}]},
  {name: '报备客户姓名', type: 'text', key: 'custName', value: '',placeholder: '支持模糊搜索',},
  {name: '报备客户电话', type: 'text', key: 'custMobile', value: '',placeholder: '只支持全号搜索',},
  {
    name: '报备客户状态', type: 'select', key: 'status', value: '', notNeedAll: true,placeholder: '', list: [//已成交  已备案  已失效  已到访
      {id: '', name: '全部'},
      {id: '10', name: '已提交'},
      {id: '30', name: '已到访'},
      {id: '20', name: '已跟进'},
      {id: '31', name: '已失效'},
      {id: '40', name: '已认筹'},
      {id: '50', name: '已认购'},
      {id: '60', name: '已签约'},
      // {id: '70', name: '已计佣'},
      // {id: '80', name: '已发佣'},
    ]
  },
]


// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  exportLoading: false,
  queryList,
  statisticsInfo: {},//统计信息
  currentItem: {},
  modalObj:{
    visible:false,
  },
  productSelf: [],
  activeKey: '1',
  // 表格数据
  tableData: {
    dataSource: [
    ],
    total: 0,
  },
  // 表格分页设置
  pageConfig: {
    pageSize: Number.parseInt(config.smallPageSizeList[0]),
    pageNo: 1
  },
  teamList:[],

}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          let {orgId='',orgName='',agentConfigId=''}=query
          // dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'search',payload: {orgId:orgId+'',agentConfigId:agentConfigId+'',orgName}}) //
          dispatch({type: 'queryOrgMemberById',payload: {orgId:orgId+'',agentConfigId:agentConfigId+'',orgName}}) //
          dispatch({type: 'getTeam',payload: {orgId}})
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    //查询操作
    *search({ payload = {} }, { call, put, select }) {
      let {queryList,tableData,pageConfig,orgId,agentConfigId} = yield select(state => state[tmpModule.namespace])
      const  pageSize = pageConfig.pageSize;
     

      //组装接口入参
      let params = {
        orgId: payload.orgId||orgId,
        agentConfigId: payload.agentConfigId||agentConfigId,
      };
      queryList.map(item => {
        // if(item.key=='status'){
        //   item.value=Number(item.value)
        // }
        params[item.key] = item.value;
      });
      //显示loading状态
      yield put({ type: 'updateStore', payload: { loading: true,btnLoading: true,  } })
      const {rows=[],success,msg,total,pageNo} = yield call(getList, { ...params, pageNo: payload.pageNo||1, pageSize, });
      console.log('数据',rows,success,msg,total,pageNo);
      yield put({type: 'updateStore', payload: {...payload, loading: false,btnLoading: false,}})
      if(!success){return false}
      let list=rows
      list.map((item,index)=>{
        // item.orgName = payload.orgName;
        item.key = index+1;
      });
      tableData = {
        dataSource: list,
        total:Number(total)
      }
      yield put({
        type: 'updateStore', payload: {
          pageConfig: {...pageConfig, ...payload},
          tableData,
        }
      })
    },
    *pageChange({ payload }, { put, select }) {
      const { pageNo, pageSize } = payload;
      const { pageConfig } =yield select(state => state[tmpModule.namespace])
      yield put({ type: 'updateStore', payload: { pageConfig: { ...pageConfig, ...payload } } });
      yield put({ type: 'search', payload: { pageNo, pageSize } });
    },
    // 查询条件重置
    *queryReset({ }, { put }) {
      yield put({ type: 'updateStore', payload: { queryList: cloneDeep(queryList) } });
      // yield put({ type: 'search' })
    },
    *getTeam({payload},{put,select,call}){
      
      let { queryList } = yield select(state => state[tmpModule.namespace])
      queryList[3].value=''
      let {orgId}=payload
      const {success,msg,data} = yield call(getTeam,orgId);
      if(success){
        queryList[3].list=data
        let start={id: '', name: '全部'}
        data.unshift(start)
        yield put({ type: 'updateStore', payload: { queryList } });
      }
    },
    //改变查询条件
    *inputChange({ payload }, { call, put, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      data.value = value;
      yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data] } });
    },
    // 详情
    *getDetail({payload }, { put,select,call}) {
      let { queryList ,tableData} = yield select(state => state[tmpModule.namespace])
      let {clueId='',index=0}=payload
      let currentItem=tableData.dataSource[index]
      if(tableData.dataSource[index]['currentItem']){
        currentItem=tableData.dataSource[index]['currentItem']
      }else{
        const {success,msg,data} = yield call(getDetail, {id:clueId });
        console.log('数据',success,msg,data,payload);
        if(success){
          currentItem=Object.assign(currentItem,data)
          tableData.dataSource[index]['currentItem']=data
        }
      }
      yield put({ type: 'updateStore', payload: { tableData,currentItem}});
    },
    //获取团队列表
    *getTeam({payload},{put,select,call}){
      let { queryList } = yield select(state => state[tmpModule.namespace])
      let {orgId}=payload
      queryList[3].value=''
      const {success,data} = yield call(getTeam,orgId);
      if(success){
        data.unshift({id:'',name:'全部'})
          queryList[3].list=data
          
      }
      yield put({ type: 'updateStore', payload: { queryList}});
    },
    // 详情
    *queryOrgMemberById({payload }, { put,select,call}) {
      let { queryList ,statisticsInfo} = yield select(state => state[tmpModule.namespace])
      let{orgId,agentConfigId,orgName}=payload
      const {success,msg,data} = yield call(queryOrgMemberById,{orgId,agentConfigId});
      statisticsInfo=data||{}
      statisticsInfo.orgName=orgName||''
      
      yield put({ type: 'updateStore', payload: { statisticsInfo}});
    },
    //导出 orgId,agentConfigId
    * exportList ({payload}, {put, call, select}) {
      let {queryList,exportLoading, orgId, agentConfigId} = yield select(state => state[tmpModule.namespace])
      //组装接口入参
      let params = {
        orgId, agentConfigId
      }
      queryList.map(item => {
        // if(item.key=='status'){
        //   item.value=Number(item.value)
        // }
        params[item.key] = item.value
      })
      yield put({type: 'updateStore', payload: {exportLoading:true}})
    
      let {data = '', success, msg} = yield call(exportList, {...params})
      yield put({type: 'updateStore', payload: {exportLoading:false}})
      if (!success) {
        message.error(msg)
        return false
      }
      
      if (data) {
        download(data)
      }
    
    },
    
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      console.log(3333,preState)
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
